/*
 * Gedigarr - Falling block game.
 * Copyright (C) 2010  BoredCorp
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package gedigarr.objects;

import gedigarr.graphics.Drawable;
import gedigarr.resources.GameResource;
import gedigarr.resources.ResourceManager;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import putil.Putil;

/**
 * A single Cell in a GameGrid. Has a default background colour, an activity 
 * state (ACTIVE/INACTIVE), and an image to display when active.
 * 
 * @author JC
 */
public class Cell implements Drawable {

  public static final int STATE_SOLID = 1 << 0;
  public static final int STATE_VISIBLE = 1 << 1;
  private int bitState = 0;
  private Color bgColour;
  private Color defaultColour = Color.BLACK;
  private Image trans = null;

  public Cell( boolean solid, boolean visible, Color bgColor ) {
    this( solid, visible, bgColor, null );
  }

  public Cell( boolean solid, boolean visible, Color bgColour, String resource ) {
    this.setSolid( solid );
    this.setVisible( visible );
    this.bgColour = ( bgColour != null ) ? bgColour : defaultColour;
    this.setSolid( solid );
    this.setVisible( visible );
    this.bgColour = (bgColour != null) ? bgColour : defaultColour;
    if ( resource != null && trans == null ) {
      GameResource gr = ResourceManager.getInstance().readImage( resource, ResourceManager.LOC_JAR );
      trans = (Image) gr.getResource();
    }
  }

  public boolean isSolid() {
    return Putil.isBitSet( this.bitState, STATE_SOLID );
  }

  public boolean isVisible() {
    return Putil.isBitSet( this.bitState, STATE_VISIBLE );
  }

  public void setSolid( boolean s ) {
    this.bitState = Putil.setBitState( this.bitState, STATE_SOLID, s );
  }

  public void setVisible( boolean v ) {
    this.bitState = Putil.setBitState( this.bitState, STATE_VISIBLE, v );
  }

  public void setState( int state ) {
    this.bitState = state;
  }

  public void setBitState( int bit, boolean b ) {
    if ( b ) {
      //turn on solid bit
      this.bitState = this.bitState | bit;
    } else {
      //turn off solid bit
      this.bitState = this.bitState & ~bit;
    }
  }

  public Color getBGColour() {
    return this.bgColour;
  }

  public void setBGColour( Color bgColour ) {
    if ( bgColour == null ) {
      this.bgColour = defaultColour;
    } else {
      this.bgColour = bgColour;
    }
  }

  public void draw( Graphics g, int x, int y, int width, int height ) {
    draw( g, x, y, width, height, this.bgColour, true );
  }

  /**
   * If bgColour is null, this Cell's background colour is used instead.
   * 
   * @param g
   * @param x
   * @param y
   * @param width
   * @param height
   * @param bgColour Forces a VISIBLE Cell to use this background colour.
   */
  public void draw( Graphics g, int x, int y, int width, int height, Color bgColour, boolean drawImage ) {
    if ( bgColour == null ) {
      bgColour = this.bgColour;
    }

    if ( trans != null ) {
      if ( isVisible() ) {
        g.setColor( bgColour );
        g.fillRect( x, y, width, height );
        if ( drawImage ) {
          g.drawImage( trans, x, y, null );
        }
      } else {
        g.setColor( defaultColour );
        g.fillRect( x, y, width, height );
      }
    } else { //image not loaded correctly.
      if ( isVisible() ) {
        g.setColor( bgColour );
      } else {
        g.setColor( defaultColour );
      }
      g.fillRect( x, y, width, height );
    }
  }
}